home *** CD-ROM | disk | FTP | other *** search
- // ObjectWindows - (C) Copyright 1992 by Borland International
-
- #define WIN30
-
- #include <owl.h>
- #include <dialog.h>
- #include <edit.h>
- #include <stdlib.h>
- #include <dclass.hpp>
- #include <string.h>
- #include <checkbox.h>
- #include "wdemo.hpp"
-
- // ********** Create Pointers to dClass Objects *********
- DBF *testdbf;
- NTX *testntx;
- NDX *testndx;
- // ****************** End dClass code *******************
-
- // ********** Field Descriptions for File Create ********
- struct FIELD_INFO test_fields[]={
- {"NAME", 'C', 25, 0},
- {"ADDRESS", 'C', 25, 0},
- {"CITY", 'C', 15, 0},
- {"STATE", 'C', 2, 0},
- {"ZIP", 'C', 5, 0},
- {"AGE", 'C', 3, 0},
- {"SALARY", 'N', 9, 2}};
- // ****************** End dClass code *******************
-
- // ******** Index Expression Evaluation Function ********
- int FAR PASCAL make_key (NTX &n)
- {
- char keybuffer[40];
- strcpy (n.keydata, n.rtn_dbf()->getfld ("name", keybuffer));
- return (0);
- }
-
- int FAR PASCAL make_key (NDX &n)
- {
- char keybuffer[40];
- strcpy (n.keydata, n.rtn_dbf()->getfld ("name", keybuffer));
- return (0);
- }
- // ****************** End dClass code *******************
-
- struct TTransferBuf
- {
- char Name[26];
- char Address[26];
- char City[16];
- char State[3];
- char Zip[11];
- char Age[4];
- char Salary[10];
- };
-
- struct TTransferBuf2
- {
- char Name[26];
- };
-
- class TDemoDialog : public TDialog
- {
- public:
- TDemoDialog(PTWindowsObject AParent, LPSTR AName);
- virtual void NewSaveButtonMsg(RTMessage Msg)
- = [ID_FIRST + ID_NEWSAVE];
- virtual void TopButtonMsg(RTMessage Msg)
- = [ID_FIRST + ID_TOP];
- virtual void BackButtonMsg(RTMessage Msg)
- = [ID_FIRST + ID_BACK];
- virtual void ForwardButtonMsg(RTMessage Msg)
- = [ID_FIRST + ID_FORWARD];
- virtual void SaveButtonMsg(RTMessage Msg)
- = [ID_FIRST + ID_SAVE];
- virtual void BottomButtonMsg(RTMessage Msg)
- = [ID_FIRST + ID_BOTTOM];
- void InitControls();
- };
-
- class TSeekDialog : public TDialog
- {
- protected:
- int Exact;
- public:
- TSeekDialog(PTWindowsObject AParent, LPSTR AName);
- virtual void SeekMsg(RTMessage Msg)
- = [ID_FIRST + ID_SEEK];
- virtual void SeekExactMsg(RTMessage Msg)
- = [ID_FIRST + ID_SEEKEXACT];
- void InitControls();
- };
-
- class TDemoWindow : public TWindow
- {
- protected:
- int Found;
- public:
- TTransferBuf DataBuf;
- TTransferBuf2 DataBuf2;
- TDemoWindow(PTWindowsObject AParent, LPSTR ATitle);
- void EraseBuffer();
- void EraseBuffer2();
- virtual void CMEdit(RTMessage Msg)
- = [CM_FIRST + CM_EDIT];
- virtual void CMAppend(RTMessage Msg)
- = [CM_FIRST + CM_APPEND];
- virtual void CMSeek(RTMessage Msg)
- = [CM_FIRST + CM_SEEK];
- virtual void CMDClass(RTMessage Msg)
- = [CM_FIRST + CM_DCLASS];
- virtual void CMDemo(RTMessage Msg)
- = [CM_FIRST + CM_DEMO];
- virtual void CMQuit(RTMessage Msg)
- = [CM_FIRST + CM_QUIT];
- void GetFields();
- void PutFields();
- void SetFound (int found) { Found = found; };
- };
-
- class TDemoApp : public TApplication
- {
- public:
- TDemoApp(LPSTR AName, HINSTANCE hInstance, HINSTANCE hPrevInstance,
- LPSTR lpCmdLine, int nCmdShow) :
- TApplication(AName, hInstance, hPrevInstance, lpCmdLine, nCmdShow) {};
- virtual void InitMainWindow();
- };
-
- char * strim (char *string)
- {
- for (int i=strlen (string)-1; i>=0; i--)
- if (string[i] != ' ')
- {
- string [++i] = '\0';
- return (string);
- }
- string[0] = '\0';
- return (string);
- }
-
- TDemoDialog::TDemoDialog(PTWindowsObject AParent, LPSTR AName)
- : TDialog(AParent, AName)
- {
- InitControls();
- }
-
- TSeekDialog::TSeekDialog(PTWindowsObject AParent, LPSTR AName)
- : TDialog(AParent, AName)
- {
- Exact = 0;
- InitControls();
- }
-
- void TDemoDialog::NewSaveButtonMsg(RTMessage)
- {
- TDemoWindow * DWPTR = (TDemoWindow *)GetApplication()->MainWindow;
- TransferData (TF_GETDATA);
-
- // ****** Get Data from Transfer Buffer and Append ******
- DWPTR->PutFields();
- DCWORD resp = testdbf->append();
- // ****************** End dClass code *******************
- if (resp)
- MessageBox (NULL,"Error Appending Record","Error",MB_OK);
-
- DWPTR->EraseBuffer();
- TransferData (TF_SETDATA);
- SetFocus (GetDlgItem (HWindow, ID_NAME));
- }
-
- void TDemoDialog::SaveButtonMsg(RTMessage)
- {
- TDemoWindow * DWPTR = (TDemoWindow *)GetApplication()->MainWindow;
- TransferData (TF_GETDATA);
-
- // ******* Get Data from Transfer Buffer and Write ******
- DWPTR->PutFields();
- DCWORD resp = testdbf->putrec();
- // ****************** End dClass code *******************
- if (resp)
- MessageBox (NULL,"Error Writing Record","Error",MB_OK);
-
- SetFocus (GetDlgItem (HWindow, ID_NAME));
- }
-
- void TDemoDialog::TopButtonMsg(RTMessage)
- {
- TDemoWindow * DWPTR = (TDemoWindow *)GetApplication()->MainWindow;
-
- // ***** Go to Top of File and Set Transfer Buffer ******
- testntx->top();
- DWPTR->GetFields();
- // ****************** End dClass code *******************
-
- strim ((char *)DWPTR->DataBuf.Name);
- strim ((char *)DWPTR->DataBuf.Address);
- strim ((char *)DWPTR->DataBuf.City);
- strim ((char *)DWPTR->DataBuf.State);
- strim ((char *)DWPTR->DataBuf.Zip);
- strim ((char *)DWPTR->DataBuf.Age);
- strim ((char *)DWPTR->DataBuf.Salary);
- TransferData (TF_SETDATA);
- SetFocus (GetDlgItem (HWindow, ID_NAME));
- }
-
- void TDemoDialog::BackButtonMsg(RTMessage)
- {
- // ****** If Already at Beginning of File, Return *******
- if (testntx->rtn_bof())
- // ****************** End dClass code *******************
- {
- MessageBeep (0);
- SetFocus (GetDlgItem (HWindow, ID_NAME));
- return;
- }
-
- TDemoWindow * DWPTR = (TDemoWindow *)GetApplication()->MainWindow;
-
- // **** Skip Back One Record and Set Transfer Buffer ****
- testntx->skip(-1L);
- DWPTR->GetFields();
- // ****************** End dClass code *******************
-
- strim ((char *)DWPTR->DataBuf.Name);
- strim ((char *)DWPTR->DataBuf.Address);
- strim ((char *)DWPTR->DataBuf.City);
- strim ((char *)DWPTR->DataBuf.State);
- strim ((char *)DWPTR->DataBuf.Zip);
- strim ((char *)DWPTR->DataBuf.Age);
- strim ((char *)DWPTR->DataBuf.Salary);
- TransferData (TF_SETDATA);
- SetFocus (GetDlgItem (HWindow, ID_NAME));
- }
-
- void TDemoDialog::ForwardButtonMsg(RTMessage)
- {
- // ********* If Already at End of File, Return **********
- if (testntx->rtn_eof())
- // ****************** End dClass code *******************
- {
- MessageBeep (0);
- SetFocus (GetDlgItem (HWindow, ID_NAME));
- return;
- }
-
- TDemoWindow * DWPTR = (TDemoWindow *)GetApplication()->MainWindow;
-
- // *** Skip Forward One Record and Set Transfer Buffer **
- testntx->skip();
- DWPTR->GetFields();
- // ****************** End dClass code *******************
-
- strim ((char *)DWPTR->DataBuf.Name);
- strim ((char *)DWPTR->DataBuf.Address);
- strim ((char *)DWPTR->DataBuf.City);
- strim ((char *)DWPTR->DataBuf.State);
- strim ((char *)DWPTR->DataBuf.Zip);
- strim ((char *)DWPTR->DataBuf.Age);
- strim ((char *)DWPTR->DataBuf.Salary);
- TransferData (TF_SETDATA);
- SetFocus (GetDlgItem (HWindow, ID_NAME));
- }
-
- void TDemoDialog::BottomButtonMsg(RTMessage)
- {
- TDemoWindow * DWPTR = (TDemoWindow *)GetApplication()->MainWindow;
-
- // **** Go to Bottom of File and Set Transfer Buffer ****
- testntx->bottom();
- DWPTR->GetFields();
- // ****************** End dClass code *******************
-
- strim ((char *)DWPTR->DataBuf.Name);
- strim ((char *)DWPTR->DataBuf.Address);
- strim ((char *)DWPTR->DataBuf.City);
- strim ((char *)DWPTR->DataBuf.State);
- strim ((char *)DWPTR->DataBuf.Zip);
- strim ((char *)DWPTR->DataBuf.Age);
- strim ((char *)DWPTR->DataBuf.Salary);
- TransferData (TF_SETDATA);
- SetFocus (GetDlgItem (HWindow, ID_NAME));
- }
-
- void TSeekDialog::SeekExactMsg(RTMessage)
- {
- Exact = (Exact ? 0 : 1);
- }
-
- void TSeekDialog::SeekMsg(RTMessage)
- {
- long rtn;
- char seekstr[26];
-
- memset (seekstr, ' ', 25);
- seekstr[25] = '\0';
-
- TDemoWindow * DWPTR = (TDemoWindow *)GetApplication()->MainWindow;
- TransferData (TF_GETDATA);
- DWPTR->SetFound (FALSE);
- memcpy (seekstr, (char *)DWPTR->DataBuf2.Name,
- strlen ((char *)DWPTR->DataBuf2.Name));
-
- // ******** Seek for the Record in the Data File ********
- rtn = testntx->seek (seekstr, Exact);
- // ****************** End dClass code *******************
-
- if (!rtn)
- {
- char buf[40];
- sprintf (buf, "seek returned %d", rtn);
- MessageBox (NULL, buf, "Seek Failed", MB_OK);
- }
- else
- {
- DWPTR->SetFound (TRUE);
- CloseWindow (IDCANCEL);
- }
- }
-
- TDemoWindow::TDemoWindow(PTWindowsObject AParent, LPSTR ATitle)
- : TWindow(AParent, ATitle)
- {
- AssignMenu("MENU_1");
- Attr.X = GetSystemMetrics(SM_CXSCREEN) / 8;
- Attr.Y = GetSystemMetrics(SM_CYSCREEN) / 8;
- Attr.H = Attr.Y * 6;
- Attr.W = Attr.X * 6;
- memset(&DataBuf, 0, sizeof(TTransferBuf));
- }
-
- void TDemoWindow::EraseBuffer()
- {
- DataBuf.Name[0] = NULL;
- DataBuf.Address[0] = NULL;
- DataBuf.City[0] = NULL;
- DataBuf.State[0] = NULL;
- DataBuf.Zip[0] = NULL;
- DataBuf.Age[0] = NULL;
- DataBuf.Salary[0] = NULL;
- }
-
- void TDemoWindow::EraseBuffer2()
- {
- DataBuf2.Name[0] = NULL;
- }
-
- void TDemoWindow::CMEdit(RTMessage)
- {
- PTDialog PD;
-
- // ***** If No Records in Database, Error and Return ****
- if (!testdbf->rtn_record_count())
- // ****************** End dClass code *******************
- {
- MessageBox (NULL, "No Records to Edit", "Edit Error", MB_OK);
- return;
- }
-
- PD = new TDemoDialog(this, "DIALOG_1");
- PD->SetTransferBuffer(&DataBuf);
-
- // ** Get the Current Record and Put in Transfer Buffer *
- GetFields();
- // ****************** End dClass code *******************
-
- strim ((char *)DataBuf.Name);
- strim ((char *)DataBuf.Address);
- strim ((char *)DataBuf.City);
- strim ((char *)DataBuf.State);
- strim ((char *)DataBuf.Zip);
- strim ((char *)DataBuf.Age);
- strim ((char *)DataBuf.Salary);
- PD->TransferData (TF_SETDATA);
- GetApplication()->ExecDialog(PD);
- }
-
- void TDemoWindow::CMAppend(RTMessage)
- {
- PTDialog PD;
- EraseBuffer();
- PD = new TDemoDialog(this, "DIALOG_4");
- PD->SetTransferBuffer(&DataBuf);
- PD->TransferData (TF_SETDATA);
- GetApplication()->ExecDialog(PD);
- }
-
- void TDemoWindow::CMSeek(RTMessage)
- {
- PTDialog PD, PD2;
-
- // ***** If No Records in Database, Error and Return ****
- if (!testdbf->rtn_record_count())
- // ****************** End dClass code *******************
- {
- MessageBox (NULL, "No Records to Seek", "Edit Error", MB_OK);
- return;
- }
-
- Found = FALSE;
- EraseBuffer2();
- PD = new TSeekDialog(this, "DIALOG_5");
- PD->SetTransferBuffer(&DataBuf2);
- PD->TransferData (TF_SETDATA);
- GetApplication()->ExecDialog(PD);
- int found = Found;
- if (found)
- {
- PD2 = new TDemoDialog(this, "DIALOG_1");
- PD2->SetTransferBuffer(&DataBuf);
-
- // *** Get the First Record and Put in Transfer Buffer **
- if (testdbf->rtn_record_count())
- {
- GetFields();
- // ****************** End dClass code *******************
-
- strim ((char *)DataBuf.Name);
- strim ((char *)DataBuf.Address);
- strim ((char *)DataBuf.City);
- strim ((char *)DataBuf.State);
- strim ((char *)DataBuf.Zip);
- strim ((char *)DataBuf.Age);
- strim ((char *)DataBuf.Salary);
- }
- PD2->TransferData (TF_SETDATA);
- GetApplication()->ExecDialog(PD2);
- }
- }
-
- void TDemoWindow::CMDClass(RTMessage)
- {
- GetApplication()->ExecDialog(new TDialog(this, "DIALOG_2"));
- }
-
- void TDemoWindow::CMDemo(RTMessage)
- {
- GetApplication()->ExecDialog(new TDialog(this, "DIALOG_3"));
- }
-
- void TDemoWindow::CMQuit(RTMessage)
- {
- SendMessage (HWindow, WM_SYSCOMMAND, SC_CLOSE, 0L);
- }
-
- void TDemoWindow::GetFields()
- {
- // ****** Get Field Data and Put in Transfer Buffer *****
- testdbf->getfld ("name", (char *)DataBuf.Name);
- testdbf->getfld ("address", (char *)DataBuf.Address);
- testdbf->getfld ("city", (char *)DataBuf.City);
- testdbf->getfld ("state", (char *)DataBuf.State);
- testdbf->getfld ("zip", (char *)DataBuf.Zip);
- testdbf->getfld ("age", (char *)DataBuf.Age);
- testdbf->getfld ("salary", (char *)DataBuf.Salary);
- // ****************** End dClass code *******************
- }
-
- void TDemoWindow::PutFields()
- {
- // *** Get Data from Transfer Buffer and Put in Fields **
- testdbf->putfld ("name", (char *)DataBuf.Name);
- testdbf->putfld ("address", (char *)DataBuf.Address);
- testdbf->putfld ("city", (char *)DataBuf.City);
- testdbf->putfld ("state", (char *)DataBuf.State);
- testdbf->putfld ("zip", (char *)DataBuf.Zip);
- testdbf->putfld ("age", (char *)DataBuf.Age);
- testdbf->putfld ("salary", (char *)DataBuf.Salary);
- // ****************** End dClass code *******************
- }
-
- void TDemoApp::InitMainWindow()
- {
- MainWindow = new TDemoWindow(NULL, Name);
- }
-
- void TDemoDialog::InitControls()
- {
- new TEdit(this, ID_NAME, 26);
- new TEdit(this, ID_ADDRESS, 26);
- new TEdit(this, ID_CITY, 16);
- new TEdit(this, ID_STATE, 3);
- new TEdit(this, ID_ZIP, 11);
- new TEdit(this, ID_AGE, 4);
- new TEdit(this, ID_SALARY, 11);
- }
-
- void TSeekDialog::InitControls()
- {
- new TEdit(this, ID_NAME, 26);
- }
-
- int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
- LPSTR lpCmdLine, int nCmdShow)
- {
- TDemoApp TestApp("dClass Demo", hInstance, hPrevInstance,
- lpCmdLine, nCmdShow);
-
- // ************** Create the dClass Objects *************
- testdbf = new DBF ("testfile", 7, test_fields);
- testntx = new NTX ("testfile", testdbf, 25, "name", 0, 0,
- IO_BUFFER_SIZE, make_key);
- testndx = new NDX ("testfile", testdbf, 25, 0, "name", 0, 0,
- IO_BUFFER_SIZE, make_key);
- // ****************** End dClass code *******************
-
- TestApp.Run();
-
- // ************** Delete the dClass Objects *************
- delete testdbf;
- delete testntx;
- delete testndx;
- // ****************** End dClass code *******************
-
- return (TestApp.Status);
- }
-